//
//  UIView+STAnimation.m
//  STRich
//
//  Created by stoneobs on 2017/7/7.
//  Copyright © 2017年 stoneobs. All rights reserved.
//

#import "UIView+STAnimation.h"
static const char floatAnimationViewkey = '\9';
@implementation UIView (STAnimation)
- (void)st_showAnimationWithType:(STAnimationType)type{
    CATransition * transion = [CATransition new];
    transion.type = [self stringWithType:type];
    transion.duration = 1;
    transion.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    [self.layer addAnimation:transion forKey:nil];
}
- (void)st_showAnimationWithType:(STAnimationType)type duration:(CFTimeInterval)duration{
    CATransition * transion = [CATransition new];
    transion.type = [self stringWithType:type];
    transion.duration = duration;
    transion.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    [self.layer addAnimation:transion forKey:nil];
}
//展示无限 旋转动画 类似 播放器
- (void)st_showCircleRoundAnimationWithKey:(NSString*)key{
    CABasicAnimation *rotationAnimation;
    
    rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    
    rotationAnimation.toValue = [NSNumber numberWithFloat:M_PI*2.0];
    
    rotationAnimation.duration = 1;
    
    rotationAnimation.repeatCount = HUGE_VALF;
    
    rotationAnimation.removedOnCompletion = NO;
    
    if (key) {
        [self.layer addAnimation:rotationAnimation forKey:key];
    }
    
    
}
- (void)st_removeAnimationWithKey:(NSString*)key{
    if (key) {
        [self.layer removeAnimationForKey:key];
    }
}
#pragma mark --漂浮动画
//随机漂浮动画 center 在这个size 区域里面运动
- (void)st_showFloatFAnimationInSize:(CGSize)size{
    NSString * point = NSStringFromCGPoint(self.center);
    objc_setAssociatedObject(self, &floatAnimationViewkey, point, OBJC_ASSOCIATION_RETAIN);
    CGPoint firstPoint = [self centerFromLastCenter:CGPointZero size:size];
    [self resetAnimationWithCenter:firstPoint size:size];
}
- (void)resetAnimationWithCenter:(CGPoint)center size:(CGSize)size{
    [UIView animateWithDuration:2.8 delay:0 options:UIViewAnimationOptionCurveLinear animations:^{
        self.center = center;
    } completion:^(BOOL finished) {
        CGPoint firstPoint = [self centerFromLastCenter:center size:size];
        [self resetAnimationWithCenter:firstPoint size:size];
    }];
    
}
- (CGPoint)centerFromLastCenter:(CGPoint)lastCenter size:(CGSize)size{
    // 随机表示，0 上边，1右边 2下边，3左边
    NSString * originPointString = objc_getAssociatedObject(self, &floatAnimationViewkey);
    CGPoint originPoint = CGPointFromString(originPointString);
    CGFloat x = 0.0;
    CGFloat y = 0.0;
    if (lastCenter.x == 0 && lastCenter.y == 0) {
        //随机一个任意边上的
        NSInteger num =   arc4random()%3;
        if (num == 0) {
            y = originPoint.y - size.height/2;
            NSInteger maxWith = size.width;
            x = arc4random()%maxWith;
            CGFloat  xfloat = arc4random()%(10) / 100;
            x = originPoint.x - size.width/2 + x + xfloat;
        }
        if (num == 1) {
            x = originPoint.x+ size.width/2;
            NSInteger maxHeight = size.height;
            y = arc4random()%maxHeight;
            CGFloat  yfloat = arc4random()%(10) / 100;
            y = originPoint.y - size.height/2 + y + yfloat;
        }
        if (num == 2) {
            y = originPoint.y + size.height/2;
            NSInteger maxWith = size.width;
            x = arc4random()%maxWith;
            CGFloat  xfloat = arc4random()%(10) / 100;
            x = originPoint.x - size.width/2 + x + xfloat;
        }
        if (num == 3) {
            x = originPoint.x- size.width/2;
            NSInteger maxHeight = size.height;
            y = arc4random()%maxHeight;
            CGFloat  yfloat = arc4random()%(10) / 100;
            y = originPoint.y - size.height/2 + y + yfloat;
        }
    }else{
        NSArray <NSNumber*>* array;
        //找出 这个边，允许
        CGFloat wucha = 0.3;
        if (originPoint.y - size.height / 2 - lastCenter.y < wucha || originPoint.y - size.height / 2 - lastCenter.y  > -wucha) {
            //上边
            array = @[@(1),@(2),@(3)];
        }
        if (originPoint.x + size.width / 2 - lastCenter.x < wucha || originPoint.x + size.width / 2 - lastCenter.x  > -wucha) {
            //右边
            array = @[@(0),@(2),@(3)];
        }
        if (originPoint.y + size.height / 2 - lastCenter.y < wucha || originPoint.y + size.height / 2 - lastCenter.y  > -wucha) {
            //下边
            array = @[@(1),@(0),@(3)];
        }
        if (originPoint.x - size.width / 2 - lastCenter.x < wucha || originPoint.x - size.width / 2 - lastCenter.x  > -wucha) {
            //右边
            array = @[@(0),@(1),@(2)];
        }
        //随机一个非 这条边上的
        NSInteger num =   array[arc4random()%(array.count-1)].integerValue;
        if (num == 0) {
            y = originPoint.y - size.height/2;
            NSInteger maxWith = size.width;
            x = arc4random()%maxWith;
            CGFloat  xfloat = arc4random()%(10) / 100;
            x = originPoint.x - size.width/2 + x + xfloat;
        }
        if (num == 1) {
            x = originPoint.x+ size.width/2;
            NSInteger maxHeight = size.height;
            y = arc4random()%maxHeight;
            CGFloat  yfloat = arc4random()%(10) / 100;
            y = originPoint.y - size.height/2 + y + yfloat;
        }
        if (num == 2) {
            y = originPoint.y + size.height/2;
            NSInteger maxWith = size.width;
            x = arc4random()%maxWith;
            CGFloat  xfloat = arc4random()%(10) / 100;
            x = originPoint.x - size.width/2 + x + xfloat;
        }
        if (num == 3) {
            x = originPoint.x- size.width/2;
            NSInteger maxHeight = size.height;
            y = arc4random()%maxHeight;
            CGFloat  yfloat = arc4random()%(10) / 100;
            y = originPoint.y - size.height/2 + y + yfloat;
        }
    }
    return CGPointMake(x, y);
}
- (NSString*)stringWithType:(STAnimationType)type{
    if (type == STAnimationTypekCATransitionPush) {
        return @"kCATransitionPush";
    }
    else if (type == STAnimationTypekCATransitionMoveIn){
        return @"kCATransitionMoveIn";
    }
    else if (type == STAnimationTypekCATransitionReveal){
        return @"kCATransitionReveal";
    }
    else if (type == STAnimationTypekCATransitionFade){
        return @"kCATransitionFade";
    }
    else if (type == STAnimationTypecube){
        return @"cube";
    }
    else if (type == STAnimationTypesuckEffect){
        return @"suckEffect";
    }
    else if (type == STAnimationTyperippleEffect){
        return @"rippleEffect";
    }
    else if (type == STAnimationTypeSTpageCurle){
        return @"pageCurl";
    }
    else if (type == STAnimationTypeSTpageUnCurl){
        return @"pageUnCurl";
    }
    else if (type == STAnimationTypeoglFlip){
        return @"oglFlip";
    }
    else if (type == STAnimationTypecameraIrisHollowOpen){
        return @"CameraIrisHollowOpen";
    }
    else if (type == STAnimationTypecameraIrisHollowClose){
        return @"CameraIrisHollowClose";
    }
    else if (type == STAnimationTypeCurlDown){
        return @"CurlDown";
    }
    else if (type == STAnimationTypeCurlUp){
        return @"CurlUp";
    }
    else if (type == STAnimationTypeFlipFromLeft){
        return @"FlipFromLeft";
    }
    else if (type == STAnimationTypeFlipFromRight){
        return @"FlipFromRight";
    }else{
        
        return @"STcameraIrisHollowClose";
    }
}

@end
